Part 2: tidy data for functional programming and iteration
2023-09-29
ggplot2::facet_wrap()tidyr::pivot_longer() | tidyr::pivot_wider()R can feel like a different galaxy
Day 1. Importing data and orientation to R / RStudio / Quarto
Day 2. Using verbs, i.e. {dplyr} functions, to subset and wrangle data
Day 3. Using {ggplot2}
Day 4. gai-assisted coding
Day 5. pivot and join
Day 6. iteration and functions
A data-first, functional, programming language
Functional programming languages mean you don’t have to write FOR loops
Rule of Thumb: If you compose an expression three or more times, write a function
Every object is either a vector or a function
All data are vectors
Vectors
Data Frames (i.e. Tibbles) are 2 dimensional vectors
Lists
Matrices
Many tidyverse functions are “vectorized” and can be invoked with an implied FOR loops
A flow control-flow-statement
♠
make_scatterplot_with_vars <- function(my_df, my_x, my_y) {
my_df |>
ggplot(aes({{my_x}}, {{my_y}})) +
geom_point()
}
starwars |>
filter(mass < 500) |>
make_scatterplot_with_vars(my_x = height, my_y = mass)
starwars |>
filter(mass < 500) |>
make_scatterplot_with_vars(height, birth_year)
cars |>
make_scatterplot_with_vars(speed, dist)In the tidyverse context we have to take into account data masking by indirectly embracing data variables
map()
Apply a function to each element of a vector or list
Can also do this in Base-R with apply(), sapply(), mapply(), lapply()
nest()| name | gender | height | mass | |
|---|---|---|---|---|
| 1 | Luke Skywalker | masculine | 172 | 77 |
| 2 | C-3PO | masculine | 167 | 75 |
| 3 | R2-D2 | masculine | 96 | 32 |
| 4 | Darth Vader | masculine | 202 | 136 |
| 5 | Leia Organa | feminine | 150 | 49 |
| 6 | Owen Lars | masculine | 178 | 120 |
| 7 | Beru Whitesun lars | feminine | 165 | 75 |
| 8 | R5-D4 | masculine | 97 | 32 |
| 9..86 | ||||
| 87 | Padmé Amidala | feminine | 165 | 45 |
# A tibble: 3 × 2
gender my_data_by_gender
<chr> <list>
1 masculine <tibble [66 × 3]>
2 feminine <tibble [17 × 3]>
3 <NA> <tibble [4 × 3]>
| gender | name | height | mass | |
|---|---|---|---|---|
| 1 | feminine | Leia Organa | 150 | 49 |
| 2 | feminine | Beru Whitesun lars | 165 | 75 |
| 3 | feminine | Mon Mothma | 150 | NA |
| 4..16 | ||||
| 17 | feminine | Padmé Amidala | 165 | 45 |